package com.limao.CacheManager;

import java.util.ArrayList;
import java.util.List;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.limao.Admin.DataClass.AdminSysMessClass;
import com.limao.DBDataClass.CrowdUserAuthClass;
import com.limao.DBDataClass.MobileStateDataClass;
import com.limao.DBDataClass.SysCidDataClass;
import com.limao.DBProvider.CrowdUserAuthProvider;
import com.limao.DBProvider.SysCidProvider;
import com.limao.DBProvider.UserOfflineProvider;
import com.limao.Model.LzPluginException;
import com.limao.Tencent.tenpay.pay.TenpayConfig;
import com.limao.Tool.CacheClass;
import com.limao.cache.BasicModule;
import com.limao.push.InviteCutThread;
import com.limao.push.pushtoAPP;



public class CrowdUserAuthCache extends BasicModule{

	
	private CrowdUserAuthProvider provider;
	private SysCidProvider cidProvider;
	private static CrowdUserAuthCache instance;


    /**
     * Returns a singleton UserManager instance.
     *
     * @return a UserManager instance.
     */
    public static CrowdUserAuthCache getInstance() {
        return instance;
    }
 
    
    /** Cache of local users. *///
    private Cache<String, CrowdUserAuthClass> userAuthDataCache;
    private Cache<String, CrowdUserAuthClass> userAuthMobileCache;
 
    
	public  Cache<String,CrowdUserAuthClass> getCache()
	{
		return 	userAuthDataCache;
	}
	
    /** Cache if a local or remote user exists. */


    public CrowdUserAuthCache() {
    	super("userAuthCacheManager");
    	userAuthDataCache = CacheFactory.createCache("cd_userAuthDataCache");
    	userAuthMobileCache = CacheFactory.createCache("cd_userAuthMobileCache");
    	
    	initProvider();
    	
    	
		//延时10分钟，主动去支付宝查过去24小时内用支付宝支付方式支付，订单状态处于待付款的支付情况
    	//TaskOneForAlipay task=new TaskOneForAlipay();
		//new java.util.Timer().schedule(task, 600*1000); //延时10分
		
  
    }
    
    
    @Override
	public void initialize() {
        super.initialize();
        instance=this;
    }
    
    
    public void ClearObjectCache(String userid)
    {
    	CrowdUserAuthClass user =userAuthDataCache.get(userid);
    	if(user!=null)
    	{
    		userAuthDataCache.remove(user.userid);
    		userAuthMobileCache.remove(user.mobile);
    	}
    }  


   
//**********************************************************************************************
    //新鲜宝 客户端用户登陆
    public CrowdUserAuthClass AuthLogin(String mobile,String password, String platform, String token,String cid)
    		throws  LzPluginException 
    {
    	
        CrowdUserAuthClass user = provider.login_User_AuthBy(mobile,password);
        if(user==null)
        	throw new LzPluginException("[用户名]或[密码]不正确");
  
 
        int p=CacheClass.getIntValue(platform);
 
      	if(p!=0)
      	{
      		SysCidDataClass uderCid=procUserCid(user.userid,cid);
      		
      		//是否首次登陆，是, 则发推送消息
       		if(UserOfflineProvider.getInstance().load_User_Offline2(CacheClass.getIntValue(user.userid)))
       		{
        			String msg="感谢您选择我们。";
        			
        			AdminSysMessClass obj=new AdminSysMessClass();
        			obj.txt=msg;
        			obj.title="感谢您";
        			obj.icontype="0";
        			obj.type="0";
        			
        			ArrayList<SysCidDataClass> list=new ArrayList<SysCidDataClass>();
        			list.add(uderCid);
        			pushtoAPP push=new pushtoAPP(list,obj.title, obj);
        			push.start();
       		}
      	}
       //当天是否已签到过了?
		if(CacheClass.getIntValue(user.signtime)!=CacheClass.getDate())
		{
			//每日签到活跃值增加  并触发狸猫豆释放;余额转至抵押金;抵押金赎回抵押订单;   
			InviteCutThread  prestige=new InviteCutThread(CacheClass.getIntValue(user.userid),2);
			prestige.start();	
		}

		//更新上线纪录
		UserOfflineProvider.getInstance().updateUserOffline(CacheClass.getIntValue(user.userid), p, 1,token);
		
		UserOfflineProvider.getInstance().insertUserLogLogin(CacheClass.getIntValue(user.userid));
		


    	userAuthDataCache.put(user.userid, user);
      	userAuthMobileCache.put(user.mobile, user);

    	return user;
    }
    
  
    //新鲜宝
    private SysCidDataClass procUserCid(String userid,String cid)
    {
    	int id=CacheClass.getIntValue(userid);
    	SysCidDataClass userCid = cidProvider.loadUserCidByCid(cid);
		if(userCid == null)
		{
			userCid = cidProvider.insesrtUserCid(cid,id);
		}else
		{

			userCid = cidProvider.updateUserCid(cid, id);
		}
    	
		return userCid;
    }
    
    //&新鲜宝 增加用户
    public CrowdUserAuthClass createUsernmae(String mobile,String password,String introducemobile,String password2) throws LzPluginException 
    {
        if(mobile==null)
        {
        	return null;
        }
        CrowdUserAuthClass obj = provider.createUsernmae(mobile,password,introducemobile,password2);
        if(obj!=null)
        {
        	userAuthDataCache.put(obj.userid, obj);
        	userAuthMobileCache.put(obj.mobile, obj);
        }

        return obj;
    }
    
 
    
    
    //&新鲜宝 修改用户密码 by id
    public CrowdUserAuthClass UPDATA_User_auth_password_ById(int id,String password,String oldpassword,String codepassword) throws LzPluginException
    {
    	if(id<1 || CacheClass.isEmpty(password))
    	{
    		return null;
    	}
    	CrowdUserAuthClass user=null;
    	user=provider.UPDATA_User_auth_password_ById(id,password,oldpassword,codepassword);
        if (user!= null) {
        	   userAuthDataCache.put(user.userid, user);
               userAuthMobileCache.put(user.mobile, user);
        }
        return user;
    }   
    
    //&新鲜宝  修改用户密码  by mobile
    public CrowdUserAuthClass UPDATA_User_auth_password_ByMobile(String mobile,String password,String codepassword) throws LzPluginException
    {
    	if(CacheClass.isEmpty(mobile) || CacheClass.isEmpty(password) || CacheClass.isEmpty(codepassword))
    	{
    		return null;
    	}
    	CrowdUserAuthClass user=null;
    	user=provider.UPDATA_User_auth_password_ByMobile(mobile,password,codepassword);
        if (user!= null) {
            userAuthDataCache.put(user.userid, user);
            userAuthMobileCache.put(user.mobile, user);
        }
        return user;
    }
    
    //&新鲜宝 修改手机号
    public CrowdUserAuthClass UPDATA_User_auth_mobile(String mobile,int id)
    {
    	if( CacheClass.isEmpty(mobile))
    	{
    		return null;
    	}
    	CrowdUserAuthClass user=null;
    	user=provider.UPDATA_User_auth_mobile(mobile,id);
        if (user!= null) {
            userAuthDataCache.put(user.userid, user);
         	userAuthMobileCache.put(user.mobile, user);
        }
        return user;
    }   
    
	//&新鲜宝   修改用户 头像
	public CrowdUserAuthClass UPDATA_User_auth_Avatar(int id,String avatar)
	{
    	if(id<1)
    	{
    		return null;
    	}
    	CrowdUserAuthClass user=null;
    	user=provider.UPDATA_User_auth_Avatar(id,avatar);
        if (user!= null) {
            userAuthDataCache.put(user.userid, user);
            userAuthMobileCache.put(user.mobile,user);
         }
        return user;
	}
    
	
	//&新鲜宝   修改用户基本信息
	public CrowdUserAuthClass UPDATA_User_auth_Base(int id,String sign)
	{
    	if(id<1)
    	{
    		return null;
    	}
    	CrowdUserAuthClass user=null;
    	user=provider.UPDATA_User_auth_Base(id,sign);
        if (user!= null) {
            userAuthDataCache.put(user.userid, user);
            userAuthMobileCache.put(user.mobile,user);
         }
        return user;
	}
	
	
    //&新鲜宝  修改用户 呢称
    public CrowdUserAuthClass UPDATA_User_auth_name(int id,String name) 
    {
    	if(id<1)
    	{
    		return null;
    	}
    	CrowdUserAuthClass user=null;
    	user=provider.UPDATA_User_auth_name(id,name);
        if (user!= null) {
            userAuthDataCache.put(user.userid, user);
            userAuthMobileCache.put(user.mobile,user);
         }
        return user;
    } 
    
	//&新鲜宝   修改提现绑定
	public CrowdUserAuthClass UPDATA_User_auth_Bank(int id,int cardtype,String cardno,String cardname,String cardbank,String cardarea) throws LzPluginException 
	{
	   	if(id<1)
    	{
    		return null;
    	}
    	CrowdUserAuthClass user=null;
    	user=provider.UPDATA_User_auth_Bank(id,cardtype,cardno,cardname,cardbank,cardarea);
        if (user!= null) {
            userAuthDataCache.put(user.userid, user);
            userAuthMobileCache.put(user.mobile,user);
         }
        return user;
	}
	
	

    //&通过手机号获取用户资料
    public CrowdUserAuthClass getUserBymobile(String mobile)
    {
    	return getUserBymobile(mobile,false);
    }
    

    public CrowdUserAuthClass getUserBymobile(String mobile,boolean LockFlag)  {
        if (CacheClass.isEmpty(mobile)) return null;

        CrowdUserAuthClass user=provider.load_User_Auth_ByMobile(mobile);
        if(user!=null)
        {
        		userAuthMobileCache.put(mobile,user);
        		userAuthDataCache.put(user.userid, user);
        }
     
        return user;
    }
    
    //&新鲜宝  手机注册与否
    public boolean isHave(String mobile)
    {
    	return provider.isHave(mobile);
    }   
    
    //&新鲜宝  手机通讯录手机注册与否
    public List<MobileStateDataClass> getMobileState(String mobile)
    {
       	return provider.getMobileState(mobile);
    }
    
       
	//指定某个用户的cid　by username
    public ArrayList<SysCidDataClass> getUserCidByMobile(String mobile)
    {
    	CrowdUserAuthClass auth=getUserBymobile(mobile);
    	if(auth!=null)
    	{
    		return getUserCidById(CacheClass.getIntValue(auth.userid));
    		
    	}
    	return null;
    }
    
  //指定某个用户的cid by id
    public ArrayList<SysCidDataClass> getUserCidById(int id){
    	
    	return cidProvider.loadUserCid(id);
    	
    }
    
    
    
    
    //&新鲜宝  通过ID获取用户资料
    public CrowdUserAuthClass getUser(int id) 
    {
    	return getUser(id,false);
    }
    
    //&新鲜宝  密码是否正确
    public boolean passIsValid(int id,String pass) 
    {
    	return provider.passIsValid(id,pass);
    }
    
    //&新鲜宝  密码是否正确
    public String getPwdMd5(int userid) 
    {
    	return provider.getPwdMd5(userid);
    }
    
  //&新鲜宝  通过ID获取用户资料
    public CrowdUserAuthClass getUser(int id,boolean LockFlag)  {
        if (id<1) {
            return null;
        }
        CrowdUserAuthClass user=null;
        if(LockFlag)//受别人锁影响
        {
        	String key=id+"_L";
            user = userAuthDataCache.get(key);
            user=getUser(id);
            if(user!=null)
            {
               userAuthDataCache.put(key, user);
            }
        }else//不受锁影响
        {
        	user = userAuthDataCache.get(id+"");
            if (user == null) {
                synchronized ((id+"").intern()) {
                    user = userAuthDataCache.get(id+"");
                    if (user == null) {
    					user = provider.load_User_Auth_ById(id);
                        if(user!=null)
                        {
                    		userAuthMobileCache.put(user.mobile,user);
                        	userAuthDataCache.put(user.userid, user);
                        }
                    }
                }
            }
        }
        if(user!=null)
        {
        	//可取
        	user.rmbforwith=user.nowrmb;
        	//盈亏率
        	user.rmbrate=CacheClass.GetPercent((CacheClass.getFloatValue(user.totalwithinrmb)+CacheClass.getFloatValue(user.nowrmb))+"", user.totalbuyscore);
        	
        	
        	user.wxappid=TenpayConfig.getAPP_ID();
        	user.wxappsecret=TenpayConfig.getAppSecret();
        	
        	user.wxsharetitel=SysSmsCacheManagr.getInstance().getWxShareTitle();
        	user.wxsharecontent=SysSmsCacheManagr.getInstance().getWxShareContent();
        	user.wxshareappid=TenpayConfig.getShareAPP_ID();
        	user.wxshareappsecret=TenpayConfig.getShareAppSecret();

        	//user.level=SyslevelManager.getInstance().loaduserLevelParam(CacheClass.getFloatValue(user.totalbuyscore))+"";
        	//user.viplevelStr = provider.getVipLevelStr(user.vipdate,user.level);

        }
        return user;
    }
    
    //&新鲜宝	理财帐户开启
	public CrowdUserAuthClass UPDATA_User_auth_Isvruser(int userid)
	{
    	if(userid<1)
    	{
    		return null;
    	}
    	CrowdUserAuthClass user=null;
    	user=provider.UPDATA_User_auth_Isvruser(userid);
        if (user!= null) {
            userAuthDataCache.put(user.userid, user);
            userAuthMobileCache.put(user.mobile,user);
         }
        return user;
	}
	
	
    //&新鲜宝	理财帐户关闭
	public CrowdUserAuthClass UPDATA_User_auth_Isvruser2(int userid)
	{
    	if(userid<1)
    	{
    		return null;
    	}
    	CrowdUserAuthClass user=null;
    	user=provider.UPDATA_User_auth_Isvruser2(userid);
        if (user!= null) {
            userAuthDataCache.put(user.userid, user);
            userAuthMobileCache.put(user.mobile,user);
         }
        return user;
	}
	
    //注册交易系统成功，用户表追加uid值
    public CrowdUserAuthClass updateAuthForUid(String userid,String uid,String pwd,String applyid)
    {
    	return provider.updateAuthForUid(userid,uid,pwd,applyid);
    }
    
    
    public CrowdUserAuthClass updateAuthForUid(String userid,String useraccount,String pwd)
    {
    	return provider.updateAuthForUid(userid,useraccount,pwd);
    }    
    
    
    public CrowdUserAuthClass UPDATA_User_mt_token(int id,String mt_token) 
    {
    	if(id<1)
    	{
    		return null;
    	}
    	CrowdUserAuthClass user=null;
    	user=provider.UPDATA_User_mt_token(id,mt_token);
        if (user!= null) {
            userAuthDataCache.put(user.userid, user);
            userAuthMobileCache.put(user.mobile,user);
         }
        return user;
    } 
    
  //**********************************************************************************************8

    private void initProvider() {
        if (provider == null ) {
            try {
                provider =CrowdUserAuthProvider.getInstance();
            }
            catch (Exception e) {
            	e.printStackTrace();
            }
        }
        if (cidProvider == null ) {
            try {
            	cidProvider =SysCidProvider.getInstance();
            }
            catch (Exception e) {
            	e.printStackTrace();
            }
        }       

    }
    
    
    
}
